package org.biz.news.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.biz.impl.BaseBiz;
import org.biz.news.IColumnBiz;
import org.hibernate.dao.IBaseDao;
import org.hibernate.entity.Column;
import org.hibernate.entity.NewsContents;
import org.hibernate.entity.NewsTitle;
import org.hibernate.entity.Suggestion;
import org.util.string.StringUtils;

/**
 * 
 * 栏目管理业务类对象
 * 
 * @Title: ColumnBiz.java
 * @Description:新闻系统(一期)
 * @Copyright: Copyright (c) 2011
 * @Company:北京学邦时代
 * @author zhangliming
 * @date Oct 30, 2012 11:44:11 AM
 * @version V4.0
 */
@SuppressWarnings("unchecked")
public class ColumnBiz extends BaseBiz implements IColumnBiz {

	private IBaseDao columnDao = null;

	public IBaseDao getColumnDao() {
		return columnDao;
	}

	public void setColumnDao(IBaseDao columnDao) {
		this.columnDao = columnDao;
	}

	/**
	 * 统计新闻数量
	 */
	public int doCountNewsByColumn(int cid) {
		return this.columnDao
				.countSqlQuery("select count(*) from news_title where red=0 and cid="
						+ cid);
	}

	/**
	 * 通过查看栏目的静态名称的方式查看新闻数据
	 */

	public Map<NewsTitle, String> selNewsByColumn(int cid, int pageNo,
			int pageSize) {
		List<NewsTitle> nts = this.columnDao.getPageBySql(NewsTitle.class,
				"select * from news_title where red=0 and cid=" + cid
						+ " order by iftop desc,posttime desc,id desc", pageNo, pageSize);

		Map<NewsTitle, String> finalResult = new LinkedHashMap<NewsTitle, String>();

		for (int i = 0; i < nts.size(); i++) {

			NewsTitle nt = (NewsTitle) nts.get(i);
			// 处理关键词
			StringBuffer result = new StringBuffer();
			//20140105 add by xy start
			// 如果描述为空，则截断内容的147字为描述
			if(!StringUtils.is_null(nt.getDescription())){
				if(nt!=null) {
					NewsContents nc = (NewsContents)this.columnDao.loadByHql("from NewsContents nc " +
							"where nc.tid=" + nt.getId());
					if(nc.getContents()!=null && StringUtils.is_null(nc.getContents())) {
						nt.setDescription(StringUtils.subHtmlAndOther(nc.getContents(), 147, "..."));
					}
				}
			}
			//20140105 add by xy end
			if (StringUtils.is_null(nt.getKeywords())) {
				String[] keys = nt.getKeywords().split(",");
				for (int j = 0; j < keys.length; j++) {
					String value = keys[j];

					//Keyword k = (Keyword) this.columnDao.loadByHql("from Keyword where keywords='" + value+ "'");
					//if(k!=null){
					//result.append("<a href=\""+ ServletActionContext.getRequest().getContextPath() + "/tags/" + k.getId()+ "/\">" + value + "</a>");
					// 20131223 add by xy start
					//}
					// 20131223 add by 
				}
			}
			finalResult.put(nt, result.toString());
		}

		return finalResult;
	}
	
	/**
	 * 通过栏目静态名称查看新闻数据
	 * 
	 * @param staticName
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public List<NewsTitle> selByColumn(int cid, int pageNo,int pageSize){
		
		return this.columnDao.getPageBySql(NewsTitle.class,
				"select * from news_title where red=0 and cid=" + cid
						+ " order by iftop desc,posttime desc,id desc", pageNo, pageSize);
	}

	/**
	 * 查看本栏目下的相关关键词
	 */
/*	public List<Keywordid> selKeywordidByCid(int cid, int pageSize) {
		return this.columnDao.getObjectBySQL(Keywordid.class,
				"select * from keywordid where cid=" + cid
						+ " group by kid limit " + pageSize);
	}*/

	/**
	 * 本栏目相关热门文档
	 */
	public List<NewsTitle> selHotArticleByCid(int cid, int pageSize) {
		return this.columnDao.getObjectBySQL(NewsTitle.class,
				"select * from news_title where red=0 and cid=" + cid
						+ " order by pv,posttime desc limit " + pageSize);
	}

	/**
	 * 本栏目带图片的热点文单,按时间排序;
	 */
	public List<NewsTitle> selHotArticleByCidPic(int cid) {

		return this.columnDao.getObjectBySQL(NewsTitle.class,
				"select * from news_title where red=0 and cid=" + cid
						+ " and ispic=1 order by posttime desc limit " + 1);
	}
	
	/** 20131223 add by xy start... */
	/**
	 * 获得其子栏目
	 * 
	 * @param columns 父栏目
	 * @return
	 */
	public List<Column> listChildColumns(Column columns) {
		if (columns == null) {
			return null;
		} else {
			return this.columnDao.query("from Column as a where a.menuColumns.id="
					+ columns.getId() + " order by a.sortWay,a.id");
		}
	}
	public List<Column> listChildColumns() {
	
			return this.columnDao.query("from Column as a  order by a.sortWay,a.id");
	
	}
	
	/**
	 * 通过查看栏目的静态名称的方式查看新闻数据
	 */
	public Map<NewsTitle, String> selNewsByColumn(String strCids, int pageNo,
			int pageSize) {

		// 目前不考虑置顶
		List<NewsTitle> nts = this.columnDao.getPageBySql(NewsTitle.class,
				"select * from news_title where red=0 and cid in(" + strCids
						+ ") order by iftop desc,posttime desc,id desc", pageNo, pageSize);

		Map<NewsTitle, String> finalResult = new LinkedHashMap<NewsTitle, String>();
		
		for (int i = 0; i < nts.size(); i++) {

			NewsTitle nt = (NewsTitle) nts.get(i);
			//20140105 add by xy start
			// 如果描述为空，则截断内容的147字为描述
			if(!StringUtils.is_null(nt.getDescription())){
				if(nt!=null) {
					NewsContents nc = (NewsContents)this.columnDao.loadByHql("from NewsContents nc " +
							"where nc.tid=" + nt.getId());
					if(nc!=null &&nc.getContents()!=null && StringUtils.is_null(nc.getContents())) {
						nt.setDescription(StringUtils.subHtmlAndOther(nc.getContents(), 147, "..."));
						System.out.println("+++++++++++++++++++++++++++");
						System.out.println(nt.getDescription());
						System.out.println("+++++++++++++++++++++++++++");
					}
				}
			}
			//20140105 add by xy end
			// 处理关键词
			StringBuffer result = new StringBuffer();
			if (nt!=null&&nt.getKeywords()!=null&&StringUtils.is_null(nt.getKeywords())) {
				String[] keys = nt.getKeywords().split(",");
				for (int j = 0; j < keys.length; j++) {
					String value = keys[j];
					System.out.println("keyword:" + nt.getKeywords());
					//Keyword k = (Keyword) this.columnDao.loadByHql("from Keyword where keywords='" + value + "'");
					//if(k!=null){
					//result.append("<a href=\""+ ServletActionContext.getRequest().getContextPath() + "/tags/" + k.getId()+ "/\">" + value + "</a>");
					//}
				}
			}
			System.out.println("+++++++++++++++++++++++++++");
			System.out.println(nt.getDescription());
			System.out.println("+++++++++++++++++++++++++++");
			finalResult.put(nt, result.toString());
		}

		return finalResult;
	}
	
	public Map<Column, List<NewsTitle>> findColumnNews(String strCids, int pageNo,
			int pageSize){
		Map<Column , List<NewsTitle>> map = new LinkedHashMap<Column,List<NewsTitle>>();
		List<Column> lstCo = this.columnDao.query("from Column where id in (" + strCids + ")  order by id" );
		if(lstCo.size() > 0 && lstCo != null && !("".equals(lstCo))){
		for(Column col : lstCo){
			List<NewsTitle> lstNT = this.columnDao.getPageBySql(NewsTitle.class,"select * from news_title where red=0 and cid = " + col.getId() + " order by iftop desc,posttime desc,id desc", pageNo, pageSize);
		   
		    	map.put(col, lstNT);
		}
		}
		System.out.println("==================>" + map.size());
		return map;
	}
	
	/**
	 * 推荐文章
	 */
	public List<NewsTitle> searchByRecommend(int limit){
		
		// 推荐，不在回收站中，普通新闻
		String strHql = "from NewsTitle n where n.ifelite!=0 and n.red=0 and n.newType=0 " +
				"order by n.ispic desc,n.ifelite desc,n.posttime desc";
		if(limit == 0){
			return columnDao.query(strHql);
		} else {
			return columnDao.query(strHql,1,limit);
		}
	}

	/**
	 * 咨询排行
	 * intDays 
	 */
	public List<NewsTitle> searchConsultingOrder(int intDays,int limit){
		
		try{
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_WEEK, intDays);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strDate = sdf.format(calendar.getTime());
			String strHql = "";
			if(intDays == 0){
				strHql = "from NewsTitle n where n.red=0 and n.newType=0 and n.posttime<='" + strDate + "' order by n.pv desc,n.posttime desc,n.id desc";
			} else {
				strHql = "from NewsTitle n where n.red=0 and n.newType=0 and n.posttime>'" + strDate + "' order by n.pv desc,n.posttime desc,n.id desc";
			}
			if(limit==0){
				return columnDao.query(strHql);
			}else{
				return columnDao.query(strHql,1,limit);
			}
		} catch(Exception e){
			return null;
		}
	}
	
	/**
	 * 最新文章
	 */
	public List<NewsTitle> listNewestArciles(int limit){

		String strHql = "from NewsTitle  nt where nt.red=0 order by nt.posttime desc,nt.id desc";
		return columnDao.query(strHql, 1, limit);
	}
	
	/**
	 * 统计新闻数据数量;
	 * 
	 * @param staticName
	 * @return
	 */
	public int doCountNewsByColumn(String strCid){
		return this.columnDao
		.countSqlQuery("select count(*) from news_title where red=0 and cid in ("
				+ strCid + ")");
	}
	/** 20131223 add by xy end... */
	
	public Column findByCidCol(Integer cid){
		String hql = "from Column where id = " + cid;
		return (Column)this.columnDao.loadByHql(hql);
	}

	@Override
	public List<Suggestion> selByQueAns(int cid, int pageNo, int pageSize) {
		return this.columnDao.getPageBySql(Suggestion.class,
				"select * from suggestion where sign=1 order by create_date desc,id desc", pageNo, pageSize);
	}

	@Override
	public int doCountQueAns() {
		return this.columnDao
				.countSqlQuery("select count(*) from suggestion where sign=1");
	}
}
